﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T.FAS.Runtime.Base.SPI;
using T.ServiceComponent.Config.Client;

namespace T.FAS.Runtime.Base.Common
{
    public class ConfigUtil
    {
        private static ConfigUtil instance = new ConfigUtil();
        private string _isMasterFasNode;
        private string _logMode;
        private string _buildServer;
        private string _orgSGDomain;
        private string _resourcePath;
        private string _commonMetaPath;
        private int _importMaxRow = -1;
        private string _agentId;
        private string _docDirName;
        private string _sysMaskConfig = "";
        private string _exprAgentId;
        private bool? _enableFASMaskStorage;
        private static readonly object _lock = new object();
        private ConfigUtil()
        {
        }

        public static ConfigUtil Instance
        {
            get
            {
                return instance;
            }
        }
        /// <summary>
        /// 是否调试
        /// </summary>       
        public bool IsDebug
        {
            get
            {
                try
                {
                    return ConfigurationManager.AppSettings["isDebug"] == "true";
                }
                catch
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 资源服务Domain
        /// </summary>       
        public string ResourceAPIDomain
        {
            get
            {
                if (string.IsNullOrEmpty(_resourcePath))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_resourcePath))
                        {
                            try
                            {
                                _resourcePath = ConfigurationManager.AppSettings["TTP.Resource.APIDomain.Http"];
                                if (string.IsNullOrEmpty(_resourcePath))
                                    _resourcePath = ConfigManager.GetConfigValue("TTP.Resource.APIDomain.Http");
                                if (string.IsNullOrEmpty(_resourcePath))
                                    throw new Exception("资源服务地址配置项TTP.Resource.APIDomain.Http不能为空");
                            }
                            catch
                            {
                                throw new Exception("资源服务地址配置项TTP.Resource.APIDomain.Http获取失败");
                            }
                        }
                    }
                }
                return _resourcePath;
            }
        }
        /// <summary>
        /// 钉钉AgentID
        /// </summary>       
        public string ExprDingTalkAgentID
        {
            get
            {
                if (string.IsNullOrEmpty(_exprAgentId))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_exprAgentId))
                        {
                            var defaultValue = "1256637521";
                            try
                            {
                                _exprAgentId = ConfigManager.GetConfigValue("Fas.Expr.DingTalkAgentID");
                                if (string.IsNullOrEmpty(_exprAgentId))
                                    _exprAgentId = defaultValue;
                            }
                            catch
                            {
                                _exprAgentId = defaultValue;
                            }
                        }
                    }
                }
                return _exprAgentId;
            }
        }
        /// <summary>
        /// 钉钉AgentID
        /// </summary>       
        public string DocDirName
        {
            get
            {
                if (string.IsNullOrEmpty(_docDirName))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_docDirName))
                        {
                            var defaultValue = "tdoc";
                            try
                            {
                                _docDirName = ConfigManager.GetConfigValue(" TTP.Resource.DocDirName");
                                if (string.IsNullOrEmpty(_docDirName))
                                    _docDirName = defaultValue;
                            }
                            catch
                            {
                                _docDirName = defaultValue;
                            }
                        }
                    }
                }
                return _docDirName;
            }
        }
        /// <summary>
        /// 资源服务Domain
        /// </summary>       
        public string GenerateCodeBuildServer
        {
            get
            {
                if (string.IsNullOrEmpty(_buildServer))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_buildServer))
                        {
                            try
                            {
                                _buildServer = ConfigurationManager.AppSettings["Fas.GenerateCode.BuildServer"];
                                if (string.IsNullOrEmpty(_buildServer))
                                    _buildServer = System.Environment.MachineName;
                            }
                            catch
                            {
                                _buildServer = System.Environment.MachineName;
                            }
                        }
                    }
                }
                return _buildServer;
            }
        }
        /// <summary>
        /// 钉钉AgentID
        /// </summary>       
        public string DingTalkAgentID
        {
            get
            {
                if (string.IsNullOrEmpty(_agentId))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_agentId))
                        {
                            var defaultValue = "1256637521";
                            try
                            {
                                _agentId = ConfigManager.GetConfigValue("TTP.Notify.DDAgentID");
                                if (string.IsNullOrEmpty(_agentId))
                                    _agentId = defaultValue;
                            }
                            catch
                            {
                                _agentId = defaultValue;
                            }
                        }
                    }
                }
                return _agentId;
            }
        }
        /// <summary>
        /// 公共元数据存储路径
        /// </summary>       
        public string CommonMetaPath
        {
            get
            {
                if (string.IsNullOrEmpty(_commonMetaPath))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_commonMetaPath))
                        {
                            try
                            {
                                _commonMetaPath = ConfigurationManager.AppSettings["Fas.GenerateCode.CommonMetaPath"];
                                if (string.IsNullOrEmpty(_commonMetaPath))
                                    _commonMetaPath = "C:/FASBuild/Common";
                            }
                            catch
                            {
                                _commonMetaPath = "C:/FASBuild/Common";
                            }
                        }
                    }
                }
                return _commonMetaPath;
            }
        }
        /// <summary>
        /// 是否主节点
        /// </summary>
        public bool IsMasterFasNode
        {
            get
            {
                if (string.IsNullOrEmpty(_isMasterFasNode))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_isMasterFasNode))
                        {
                            var defaultValue = "false";
                            try
                            {
                                _isMasterFasNode = ConfigManager.GetConfigValue("FAS.IsMasterNode");
                                if (string.IsNullOrEmpty(_isMasterFasNode))
                                    _isMasterFasNode = defaultValue;
                            }
                            catch
                            {
                                _isMasterFasNode = defaultValue;
                            }
                        }
                    }
                }
                return _isMasterFasNode.ToLower() == "true";
            }
        }
        /// <summary>
        /// 资源服务Domain
        /// </summary>       
        public string LogType
        {
            get
            {
                if (string.IsNullOrEmpty(_logMode))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_logMode))
                        {
                            var defaultValue = "DB";
                            try
                            {
                                _logMode = ConfigManager.GetConfigValue("Fas.Base.FasLogMode");
                                if (string.IsNullOrEmpty(_logMode))
                                    _logMode = defaultValue;
                            }
                            catch
                            {
                                _logMode = defaultValue;
                            }
                        }
                    }
                }
                return _logMode;
            }
        }
        /// <summary>
        /// orgSG地址
        /// </summary>       
        public string FasOrgSGDomain
        {
            get
            {
                if (string.IsNullOrEmpty(_orgSGDomain))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_orgSGDomain))
                        {
                            var defaultValue = "http://localhost:9080";
                            try
                            {
                                _orgSGDomain = ConfigManager.GetConfigValue("Fas.Org.SGDomain");
                                if (string.IsNullOrEmpty(_orgSGDomain))
                                    _orgSGDomain = defaultValue;
                            }
                            catch
                            {
                                _orgSGDomain = defaultValue;
                            }
                        }
                    }
                }
                return _orgSGDomain;
            }
        }
        /// <summary>
        /// 查询方案修改权限
        /// </summary>       
        public string ModifyAuthUser
        {
            get
            {
                try
                {
                    var str = ConfigManager.GetConfigValue("Fas.Base.QueryPlan.ModifyAuth");
                    if (string.IsNullOrEmpty(str))
                        return "";
                    else
                        return str;
                }
                catch
                {
                    return "";
                }
            }
        }
        /// <summary>
        /// 导入条数配置
        /// </summary>       
        public int ImportMaxRow
        {
            get
            {
                if (_importMaxRow == -1)
                {
                    lock (_lock)
                    {
                        if (_importMaxRow == -1)
                        {
                            try
                            {
                                int.TryParse(ConfigManager.GetConfigValue("Fas.Base.ImportMaxRow"), out _importMaxRow);
                            }
                            catch
                            {
                                _importMaxRow = 0;
                            }
                        }
                    }
                }
                return _importMaxRow;
            }
        }
        /// <summary>
        /// 是否启用fas掩码存储
        /// </summary>
        public bool EnableFASMaskStorage
        {
            get
            {
                if (_enableFASMaskStorage == null)
                {
                    lock (_lock)
                    {
                        if (_enableFASMaskStorage == null)
                        {
                            try
                            {
                                _enableFASMaskStorage = bool.Parse(ConfigManager.GetConfigValue("Fas.Base.EnableMaskStorage"));
                            }
                            catch
                            {
                                _enableFASMaskStorage = false;
                            }
                        }
                    }
                }
                return _enableFASMaskStorage.Value;
            }
        }

        /// <summary>
        /// SYS的公共掩码配置
        /// </summary>       
        public string SYSPublicMaskConfigStr
        {
            get
            {
                if (string.IsNullOrEmpty(_sysMaskConfig))
                {
                    lock (_lock)
                    {
                        if (string.IsNullOrEmpty(_sysMaskConfig))
                        {
                            try
                            {
                                _sysMaskConfig = ConfigManager.GetConfigValue("BIZ.SYS.DesensitizationConfig");
                            }
                            catch
                            {
                                _sysMaskConfig = null;
                            }
                            if (string.IsNullOrEmpty(_sysMaskConfig))
                            {
                                _sysMaskConfig = "noVal";
                            }
                        }
                    }
                }
                return _sysMaskConfig;
            }
        }

        /// <summary>
        /// SYS的公共掩码配置
        /// </summary>       
        public List<SYSMaskConfig> SYSPublicMaskConfig
        {
            get
            {
                if (SYSPublicMaskConfigStr != "noVal")
                {
                    try
                    {
                        List<SYSMaskConfig> list = JsonConvert.DeserializeObject<List<SYSMaskConfig>>(SYSPublicMaskConfigStr);
                        return list;
                    }
                    catch (Exception)
                    {
                        return null;
                    }
                }
                return null;
            }
        }

        private string _teableConfig;
        /// <summary>
        /// 在线编辑表格服务地址
        /// </summary>       
        public string FasTeableConfig
        {
            get
            {
                try
                {
                    _teableConfig = ConfigurationManager.AppSettings["FasTeableConfig"];
                    if (string.IsNullOrEmpty(_teableConfig))
                        _teableConfig = ConfigManager.GetConfigValue("FAS.Control.TeableConfig");
                }
                catch
                {
                    _teableConfig = "{\"TeableUrl\":\"http://10.213.0.40:12200\",\"TeableAuthorization\":\"teable_accfs5q5xYp3gmM6EaU_bDYpHoxwPZ+3DbhTYbjygHnJRATp9zzq7YH985Uqzkw=\"}";// DW-02测试环境地址及Token
                }
                return _teableConfig;
            }
        }
    }
}
